#ifndef NET_ACCEPTOR_H_
#define NET_ACCEPTOR_H_
#include <fermat/event/channel.h>
#include <fermat/net/tcp_socket.h>
#include <fermat/common/noncopyable.h>
#include <memory>
#include <functional>


namespace fermat {

namespace event {
class EventLoop;
typedef std::shared_ptr<EventLoop> EventLoopPtr;
}

namespace net {
class SocketAddress;

class Acceptor : fermat::noncopyable {
 public:
  typedef std::function<void (int sockfd,
                                 SocketAddress&)> NewConnectionCallback;

  Acceptor(fermat::event::EventLoopPtr loop,  const SocketAddress& listenAddr, bool reuseport );
  ~Acceptor();

  void setNewConnectionCallback(const NewConnectionCallback& cb)
  { _newConnectionCallback = cb; }

  bool listenning() const { return _listenning; }
  void listen();

 private:
  void handleRead();

  fermat::event::EventLoopPtr   _loop;
  Socket                        _acceptSocket;
  fermat::event::Channel        _acceptChannel;
  NewConnectionCallback         _newConnectionCallback;
  bool                          _listenning;
  int                           _idleFd;
};
}
}


#endif
